var CompanysProfile = {

    init:function () {
        var self = this;

        Calendar.setup({
            inputField : "date_of_establishment",
            trigger    : "date_of_establishment_btn",
            onSelect   : function() { this.hide() },
            dateFormat: "%B %d, %Y",
            showTime: false,
            animation: true
        });

        /* When the user clicks on the save button of the configuration window*/

        $('#save-btn').click(function () {
            self.save();
        });

        $('#org-form').validationEngine();
    },


    save:function () {
        var self = this;
        /* Validate the form*/
        var validationStatus = $('#org-form').validationEngine({returnIsValid:true});
        /* When the Validation Status is true meaning that the data input are correct then the data can be processed. */
        if (validationStatus) {
            jLib.showFormLoading();
            /* Preparation of the data to send to the server to be processed. Here we retrieve the data from the login form. */
            var query = 'model=Organisation&' + $('#org-form').serialize();
            var url = $('#org-form').attr('action');
            /* Send the data to the server and handle the server response */
            $.ajax({
                url:url,
                data:query,
                dataType:'json',
                type:'POST',
                success:function (response) {
                    var txt = '';
                    if (typeof response.mesg == 'object') {
                        for (megTxt in response.mesg) {
                            txt += response.mesg[megTxt] + '<br />';
                        }
                    }
                    else {
                        txt = response.mesg
                    }
                    jLib.hideFormLoading();
                    //* When everything went on smoothly on the server redirect the user to the appropriate page.*//*
                    jLib.message(txt, 'success');
                    if (response.code === 0) {

                        var content = txt;
                        //jLib.jalert(content,'green');
                    }
                    //* When there are Errors *//*
                    else if (response.code === 1) {
                        jLib.serverError(txt);
                    }
                },
                error:function (xhr) {
                    console.log(xhr.responseText);
                    jLib.serverError('Server Error');
                }
            });
        }
    }
};

/* when the page is loaded */
$(document).ready(function () {
    CompanysProfile.init();
});